﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Globalization;
using System.Xml;
using System.IO;

using AnOtherBlog.Core.Extensions;

namespace AnOtherBlog.Core
{
	public static class Util
	{
		
		/// <summary>
		/// Entrega el URL absoluto del sitio
		/// </summary>
		/// <value></value>
		public static Uri WebRootAbsolute
		{
			get
			{
				if (HttpContext.Current == null)
					throw new System.Net.WebException("El actual contexto de Http es inválido");

				return new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + WebRootRelative);
			}
		}

		/// <summary>
		/// Entrega el URL relativo (sin el protocolo ni el host) de sitio, hace falta para que funcione en el directorio virtual
		/// </summary>
		/// <value>Cadena con el URL terminado en '/'.</value>
		public static string WebRootRelative
		{
			get
			{
				return VirtualPathUtility.ToAbsolute("~/");
			}
		}

		/// <summary>
		/// Convierte un URL relativo a absoluto
		/// </summary>
		public static Uri ConvertToAbsolute(string urlRelative)
		{
			if (String.IsNullOrEmpty(urlRelative))
				throw new ArgumentNullException("urlRelative");

			string urlAbsolute = WebRootAbsolute.ToString();
			int index = urlAbsolute.LastIndexOf(WebRootRelative.ToString());

			return new Uri(urlAbsolute.Substring(0, index) + urlRelative);
		}


		/// <summary>
		/// Obtiene un parametro del query string
		/// </summary>
		/// <param name="sParam">Nombre del parametro</param>
		/// <returns>Valor del parametro si existe o un string vacio</returns>
		public static string GetParameter(string sParam)
		{
			string sValue = HttpContext.Current.Request[sParam];
			return sValue ?? string.Empty;
		}



		/// <summary>
		/// Strips all illegal characters from the specified title.
		/// 
		/// Se utiliza para convertir una cadena de texto en un URL válido
		/// 
		/// </summary>
		public static string RemoveIllegalCharacters(string text)
		{
			if (string.IsNullOrEmpty(text))
				return text;

			text = text.Replace(":", string.Empty);
			text = text.Replace("/", string.Empty);
			text = text.Replace("?", string.Empty);
			text = text.Replace("#", string.Empty);
			text = text.Replace("[", string.Empty);
			text = text.Replace("]", string.Empty);
			text = text.Replace("@", string.Empty);
			text = text.Replace(".", string.Empty);
			text = text.Replace(",", string.Empty);
			text = text.Replace("\"", string.Empty);
			text = text.Replace("&", string.Empty);
			text = text.Replace("'", string.Empty);
			text = text.Replace(" ", "-");
			text = RemoveDiacritics(text);
			text = RemoveExtraHyphen(text);

			return HttpUtility.UrlEncode(text).Replace("%", string.Empty);
		}

		private static string RemoveExtraHyphen(string text)
		{
			if (text.Contains("--"))
			{
				text = text.Replace("--", "-");
				return RemoveExtraHyphen(text);
			}
			return text;
		}

		private static string RemoveDiacritics(string text)
		{
			string normalized = text.Normalize(NormalizationForm.FormD);
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < normalized.Length; i++)
			{
				Char c = normalized[i];
				if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
					sb.Append(c);
			}
			return sb.ToString();
		}

	}
}
